package com.gxa.eshop.service.impl;

import com.gxa.eshop.constraints.RedisKey;
import com.gxa.eshop.enums.ErrorCode;
import com.gxa.eshop.exception.BusinessException;
import com.gxa.eshop.pojo.User;
import com.gxa.eshop.repository.UserRepository;
import com.gxa.eshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author bill
 * @date 2022/1/20 9:50
 */
@org.apache.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserRepository userRepository;


    public static final String LOGIN="sfvasfewsjkfbacvasjcasjcj";

    @Override
    public Map<String, String> login(String phone, String code) throws BusinessException {

        //一键登录

        //1.获取服务器存储的验证码
        String serverCode= (String) redisTemplate.boundValueOps(RedisKey.LOGIN_SMS +phone).get();

        if(StringUtils.isEmpty(serverCode)){
            //验证码失效
            throw  new BusinessException(ErrorCode.LOGIN_SMS_EXPIRE);
        }
        if(!code.equals(serverCode)){
            //验证码输入错误
            throw  new BusinessException(ErrorCode.LOGIN_SMS_ERROR);
        }

        User user = userRepository.findUserByPhone(phone);
        if(user==null){
            //匿名注册
            System.out.println("用户没有账号，匿名注册:"+phone);
            user=new User();
            user.setPhone(phone);
            user.setUsername("用户:"+phone);
            user.setStatus(1);//正常账号

            userRepository.save(user);

        }

        if(user.getStatus()!=1){
            throw new BusinessException(ErrorCode.USER_NO);
        }

        //我怎么知道他是手机端?

        //获取上次的token,获取后移除上一次的登录信息
        if(!StringUtils.isEmpty(user.getToken())){
            redisTemplate.delete(LOGIN+user.getToken());
        }

        //生成token，存储登录信息
        String token= UUID.randomUUID().toString().replace("-","");

        //处理登录

        // 设置ip
        user.setLastLoginTime(new Date());
        user.setToken(token);
        userRepository.save(user);//更新ip和登录时间


        redisTemplate.boundValueOps(LOGIN+token).set(user);
        redisTemplate.boundValueOps(LOGIN+token).expire(60*60*2, TimeUnit.SECONDS);

        Map<String,String> result=new HashMap<>();
        result.put("token",token);

        return result;
    }

    @Override
    public User queryUserByToken(String token) throws BusinessException {

        User user = (User) redisTemplate.boundValueOps(LOGIN + token).get();
        if (ObjectUtils.isEmpty(user)) {
            System.out.println("无效token");
            throw new BusinessException(ErrorCode.TOKEN_NO);
        }

        //要更新失效时间
        redisTemplate.boundValueOps(LOGIN+token).expire(60*60*2, TimeUnit.SECONDS);

        return user;
    }
}